/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session.TX;

import Helper.*;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.Stateful;

/**
 *
 * @author Randall Chia
 */
@Stateful
public class ShoppingCartSB implements ShoppingCartSBRemote {

    public ShoppingCartSB(){}
    
    @PostConstruct
    public void cartCreatedNotifier(){
        System.out.println(">>> ShoppingCartBean instantiated in this session");
    }
    
    
    private List<Helper_LineItem> cart = new ArrayList<Helper_LineItem>();
    
    

    public void cart_removeAllItems(){
        cart.clear();
    }//------------------------------------------------------------------------------------------
    
    
    public void cart_addSingleItem(Helper_LineItem item){
        System.out.println(">>> Shopping Cart Bean: Adding single item to cart...");
        int amountToAdd = item.getItem_quantity();
        
        for(Helper_LineItem o : cart){
            
            //check if the item is already inside the cart (look for a matching ID)
            if(o.getItem_ID() == item.getItem_ID()){    
                int newQuantity = o.getItem_quantity() + amountToAdd;
                System.out.println("Item found in cart, current QTY: " +  o.getItem_quantity() + "   QTY to add: " +  amountToAdd);
             
                Helper_LineItem updatedItem = new Helper_LineItem(
                         o.getItem_ID(),
                         o.getItem_name(),
                         newQuantity,
                         o.getItem_price(),
                         o.getSeller(),
                        o.getItem_outletID(),
                        o.getItem_outletName()
                        );
                
                cart.set(cart.indexOf(o), updatedItem);
                
                

                return;
            }
        }
      
        System.out.println("Item doesn't exist in cart: " + item);
        cart.add(item);
        System.out.println("Added to cart: " + item);
        
        System.out.println("***************************" );
        for(Helper_LineItem o : cart){
            System.out.println(o);
        }
        System.out.println("***************************" );
    }//------------------------------------------------------------------------------------------
    
    
    public void cart_addMultipleItems(List<Helper_LineItem> addStuff){
        
        System.out.println(">>> Shopping Cart Bean: Adding multiple items to cart...");
        for(Helper_LineItem o : addStuff){
            cart_addSingleItem(o);
        }
        System.out.println(">>> Shopping Cart Bean: All items added");        
    }//------------------------------------------------------------------------------------------
    
    
    public double getSubtotal(){
        double subtotal = 0;
        for(Helper_LineItem o : cart){
            subtotal += o.getItem_price()*o.getItem_quantity();
        }
        return subtotal;
    }//------------------------------------------------------------------------------------------

    
   

    /**
     * @return the cart
     */
    public List<Helper_LineItem> getCart() {
        return cart;
    }

    /**
     * @param cart the cart to set
     */
    public void setCart(List<Helper_LineItem> cart) {
        this.cart = cart;
    }
    
    
    
    
    
    
}
